let answersApp = new Vue({
    el:"#answersApp",
    data:{
        answers:[],
        comment:{}
    },
    methods:{
        loadAnswers(){
            axios.get('/portal/answer/question/'+questionId)
                .then(result=>{
                    let r = result.data
                    console.log(r)
                    if(r.code == OK){
                        this.answers = r.data
                        updateDuration(this.answers)
                    }
                })
        },
        postComment(answerId){
            let content = $("#content"+answerId).val()
            axios.post('/portal/comment/postcomment',{
                answerId:answerId,
                content:content
            })
                .then(result=>{
                    let r =result.data
                    console.log(r)
                    if(r.code==CREATED){
                        $("#content"+answerId).val("")
                        let comment = r.data
                        for(let i = 0;i < this.answers.length;i++){
                            let answer = this.answers[i]
                            if(answer.id == answerId){
                                answer.comments.unshift(comment)
                            }
                        }
                    }
                    })
        },
        updateComment(commentId){
            let content = $("#textareaComment"+commentId).val()
            axios.post('/portal/comment/updateComment',{
                id:commentId,
                content:content
            })
                .then(result=>{
                    let r = result.data
                    console.log(r)
                    if(r.code == CREATED){
                        $("#updateContent"+commentId).text(content)
                        $("#textareaComment"+commentId).val("")
                    }
                })
        },
        removeComment(commentId,idx,ansidx){
            axios.get('/portal/comment/delete/'+commentId)
                .then(result=>{
                    let r = result.data
                    console.log(r)
                    if(r.code === OK){
                        this.answers[ansidx].comments.splice(idx,1)
                    }
                })
        },
        answerSolved(answerId){
            axios.get('/portal/answer/solved/'+answerId+'/'+questionId)
                .then(result=>{
                    let r = result.data
                    console.log(r)
                    if(r.code == OK){
                        for (let i = 0; i < this.answers.length; i++) {
                            let answer = this.answers[i]
                            if(answerId == answer.id){
                                answer.acceptStatus=1
                            }
                        }
                    }
                })
        }
    },
    created(){
        this.loadAnswers()
    }

})